'use strict';

let Fiber = require('fibers');
let lib = {
    util: require('../modules/lib/util')
};
let models = require('../modules/models');

let Device = models.Device;
let School = models.School;

let DeviceDao = models.DeviceDao;
let SchoolDao = models.SchoolDao;

new Fiber(main).run();

function createDevicesForSchools(data) {
    DeviceDao.remove({});

    Object.keys(data).forEach(schoolName => {
        let school = SchoolDao.findOne({
            name: schoolName
        });

        let devices = data[schoolName];
        school.devices = createDevices(devices);
        SchoolDao.update(school);
    });
}

function createDevices(devicesData) {
    let devices = devicesData.map(deviceData => {
        let device = new Device(deviceData);
        DeviceDao.create(device);

        return device;
    });

    return devices;
}

function main() {
    createDevicesForSchools({
        '合肥168中学': [{
            name: '168中学西门',
            serialId: '168201601'
        }, {
            name: '168中学南门',
            serialId: '168201602'
        }, {
            name: '168中学北门',
            serialId: '168201603'
        }],
        '合肥市第四十二中学': [{
            name: '合肥42中校门',
            serialId: '53040002'
        }],
        '合肥市绿怡小学': [{
            name: '合肥绿怡小学校门',
            serialId: '53040003'
        }]
    });

    process.exit();
}
